﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BenzInfo.Hockey.Grenadiers.Models;
using System.Drawing;
using System.Drawing.Imaging;
using BenzInfo.Hockey.Grenadiers.Utils;

namespace BenzInfo.Hockey.Grenadiers.Controllers
{ 
    public class SurveyController : BaseController
    {
        private GrenadiersContext db = new GrenadiersContext();

        private const string QuestionKey = "Questions";

        //
        // GET: /Ad/

        public ActionResult Edit()
        {
            Question question = db.Questions.Where(p => p.Key == QuestionKey).Single();

            return PartialView(question);
        }


        //
        // POST: /Ad/Edit/5

        [HttpPost, ValidateInput(false)]
        public ActionResult Edit(Question question, HttpPostedFileBase inputFile, string nopicture)
        {
            question.Picture = null;

            Question questiondb = db.Questions.Find(question.Id);

            question.UserModifiedBy = ViewBag.CurrentUser;
            question.DateModified = DateTime.Now;

            ModelState.Remove("UserCreatedBy");
            ModelState.Remove("UserModifiedBy");

            TryUpdateModel(questiondb);

            if (ModelState.IsValid)
            {
                questiondb.Picture = question.Picture; // Quickfix...
                db.Questions.Attach(questiondb);
                db.Entry(questiondb).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index","Home");
            }

            return RedirectToAction("Index", "Home");
        }
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}